﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ATLASConfNotes;
using t_TestingLibrary;
using System.ComponentModel.Composition;
using PaperRenderingInterfaces;
using System.IO;
using RenderEngineInterfaces;
using DeepZoomPivotConstructor.VisualLib;
using System.Drawing;
using PaperRenderingEngine;
using t_PivotGeneratorTestHelpers;
using DeepZoomPivotConstructor.PivotLib;
using System.Diagnostics;

namespace t_ATLASConfNotes
{
    [TestClass]
    public class t_ATLASParseConfNotePage
    {
        [TestInitialize]
        public void Setup()
        {
            Utilities.MyClassInit();
        }

        [TestCleanup]
        public void Cleanup()
        {
            Utilities.MyClassDone();
        }

        [Export(typeof(IRenderEngine))]
        public class MyDummyRender : IRenderEngine
        {
            public System.Threading.Tasks.Task<DeepZoomPivotConstructor.VisualLib.Visual> RenderToFuture(object what, Type[] disallowRenderPlugins = null, bool forceAsComposableUnit = false)
            {
                throw new NotImplementedException();
            }

            public DeepZoomPivotConstructor.VisualLib.Visual Render(object what, Type[] disallowRenderPlugins = null, bool forceAsComposableUnit = false)
            {
                var result = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
                if (forceAsComposableUnit)
                {
                    result.SetAsComposableUnit(null);
                }
                return result;
            }
        }


        [TestMethod]
        public void TestCtor()
        {
            ATLASParseConfNotePage ap = new ATLASParseConfNotePage();
        }

        [TestMethod]
        public void SimpleTestParse()
        {
            ATLASParseConfNotePage target = new ATLASParseConfNotePage();
            var laf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_ATLASConfNotes\\ATLAS-CONF-2010-010.html") };
            Utilities.AddPart(laf);
            Utilities.AddAssemblyForType(typeof(ParseTableForImages));
            Utilities.AddPart(new ParseTableForImages());
            Utilities.AddPart(new ParseTableRowForImages());
            Utilities.AddPart(new CellInfoConverter());
            Utilities.AddPart(new MyDummyRender());
            Utilities.Compose(target);

            var result = target.Render("https://atlas.web.cern.ch/Atlas/GROUPS/PHYSICS/CONFNOTES/ATLAS-CONF-2010-010/");

            Assert.AreEqual(5, result.Count(), "Didn't find expected number of images!");
        }

        [TestMethod]
        public void SimpleTestParseWithRealLiveHTML()
        {
            ATLASParseConfNotePage target = new ATLASParseConfNotePage();
            Utilities.AddPart(new WebAccess());
            Utilities.AddAssemblyForType(typeof(ParseTableForImages));
            Utilities.AddPart(new ParseTableForImages());
            Utilities.AddPart(new ParseTableRowForImages());
            Utilities.AddPart(new CellInfoConverter());
            Utilities.AddPart(new MyDummyRender());
            Utilities.Compose(target);

            var result = target.Render("https://atlas.web.cern.ch/Atlas/GROUPS/PHYSICS/CONFNOTES/ATLAS-CONF-2010-010/");

            Assert.AreEqual(5, result.Count(), "Didn't find expected number of images!");
        }

        [TestMethod]
        public void TestForBadCharacters()
        {
            ATLASParseConfNotePage target = new ATLASParseConfNotePage();
            var laf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_ATLASConfNotes\\ATLAS-CONF-2010-018.html") };
            Utilities.AddPart(laf);
            Utilities.AddAssemblyForType(typeof(ParseTableForImages));
            Utilities.AddPart(new ParseTableForImages());
            Utilities.AddPart(new ParseTableRowForImages());
            Utilities.AddPart(new CellInfoConverter());
            Utilities.AddPart(new MyDummyRender());
            Utilities.Compose(target);

            var result = target.Render("https://atlas.web.cern.ch/Atlas/GROUPS/PHYSICS/CONFNOTES/ATLAS-CONF-2010-010/");
            var results = result.ToArray();

            Assert.AreEqual(12, results.Count(), "Didn't find expected number of images!");
            ImageChecks.CheckForStandardMetadata(results);
            ImageChecks.CheckNameAndDescription(results);
        }

        [TestMethod]
        public void TestForTwoLineTitles()
        {
            ATLASParseConfNotePage target = new ATLASParseConfNotePage();
            var laf = new LoadMultipleFiles();
            laf.FileFinder = u => {
                if (u.OriginalString.EndsWith(".info")) {
                    return new FileInfo("..\\..\\..\\t_ATLASConfNotes\\ATLAS-CONF-2010-022.txt");
                }
                else
                {
                    return new FileInfo("..\\..\\..\\t_ATLASConfNotes\\ATLAS-CONF-2010-022.html");
                }
            };

            Utilities.AddPart(laf);
            Utilities.AddAssemblyForType(typeof(ParseTableForImages));
            Utilities.AddPart(new ParseTableForImages());
            Utilities.AddPart(new ParseTableRowForImages());
            Utilities.AddPart(new CellInfoConverter());
            Utilities.AddPart(new MyDummyRender());
            Utilities.Compose(target);

            var result = target.Render("https://atlas.web.cern.ch/Atlas/GROUPS/PHYSICS/CONFNOTES/ATLAS-CONF-2010-022/");
            var results = result.ToArray();

            ImageChecks.CheckForStandardMetadata(results);
            ImageChecks.CheckNameAndDescription(results);

            ///
            /// Make sure the title has the second line
            /// 

            var titles = (from img in results
                         from f in img
                         where f.Name == "Note Title"
                         select ((PivotMetadataString)f).Value);

            foreach (var t in titles)
            {
                Trace.WriteLine("Title: " + t);
            }
            Assert.AreEqual(0, titles.Where(s => !s.Contains("sqrt")).Count(), "All titles should have sqrt in them!");
        }
    }
}
